package colorword;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ColorWord extends JFrame implements ItemListener {
    Color c;
    JTextField text_component;
    JRadioButton red_button;
    JRadioButton blue_button;
    JRadioButton black_button;
    JPanel panel;
    ButtonGroup group;

    public ColorWord () {
        super("ColorWord");
        this.setVisible(true);
        this.setSize(600, 600);
        this.setLayout(new FlowLayout());
        this.text_component = new JTextField("Welcome to java world", 12);
        this.add(text_component);
        this.red_button = new JRadioButton("red");
        this.blue_button = new JRadioButton("blue");
        this.black_button = new JRadioButton("black");
        this.group = new ButtonGroup();
        this.panel = new JPanel();
        this.panel.setLayout(new GridLayout(1, 3));
        this.group.add(this.red_button);
        this.group.add(this.blue_button);
        this.group.add(this.black_button);
        this.panel.add(this.red_button);
        this.panel.add(this.blue_button);
        this.panel.add(this.black_button);
        this.add(this.panel);
        this.pack();
        this.red_button.addItemListener(this);
        this.blue_button.addItemListener(this);
        this.black_button.addItemListener(this);
        this.black_button.setSelected(true);
        this.c = Color.black;
        this.text_component.setForeground(this.c);
    }
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == this.red_button) {
            this.c = Color.red;
        }
        else if (e.getSource() == this.blue_button) {
            this.c = Color.blue;
        }
        else {
            this.c = Color.black;
        }
        this.text_component.setForeground(this.c);
    }
    public static void main (String[] args) {
        ColorWord app = new ColorWord();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
